<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>商品首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//unpkg.com/layui@2.11.2/dist/css/layui.css" rel="stylesheet">
    <style>
        table {
            width: 1200px;
            margin: 20px;
        }

        #demo-laypage-normal-1 {
            width: 500px;
            margin: auto;
        }

        .goods>div:nth-child(1) {
            padding: 20px;
            width: 400px;
            border: 1px solid black;
            margin: auto;
            text-align: center;
            border-radius: 5px;
            margin-bottom: 30px;
        }

        .goods tbody td {
            text-align: center;
        }

        .goods tbody td button {
            border: none;
            border-radius: 2px;
            background-color: rgb(242, 240, 171);
            padding: 3px;
            box-sizing: border-box;
            margin: 3px;
        }

        .add_btn {
            width: 60px;
            height: 30px;
            font-size: 15px;
        }

        .add_btn~input {
            height: 20px;
        }

        .query_btn {
            width: 40px;
            height: 25px;
            margin-left: 5px;
        }

        /* 
        .tab.active {
            background-color: #16baaa;
            color: white
        } */

        .tab {
            display: none;
        }

        .tab.show {
            display: block;
        }
    </style>
</head>

<body>
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">商品管理系统</div>
            <!-- 头部区域（可配合layui 已有的水平导航） -->
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a href="javascript:;">
                        <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                        欢迎<span></span>
                    </a>
                    <!-- <dl class="layui-nav-child">
                        <dd><a href="javascript:;">Your Profile</a></dd>
                        <dd><a href="javascript:;">Settings</a></dd>
                        <dd><a href="javascript:;">Sign out</a></dd>
                    </dl> -->
                </li>
                <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                    <a href="javascript:;">
                        <i class="layui-icon layui-icon-more-vertical"></i>
                    </a>
                </li>
            </ul>
        </div>
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test">

                    <li class="layui-nav-item layui-nav-itemed tabBtn">
                        <a class="" href="javascript:;">商品管理</a>
                    </li>

                    <li class="layui-nav-item tabBtn">
                        <a href="javascript:;">商品可视化</a>
                    </li>
                    <li class="layui-nav-item tabBtn"><a href="javascript:;">个人中心</a></li>
                    <!-- <li class="layui-nav-item"><a href="javascript:;">the links</a></li> -->
                </ul>
            </div>
        </div>
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <div style="padding: 15px;">
                <div class="layui-card layui-panel">
                    <div class="layui-card-header">
                        <div class="goods tab">
                            <div>
                                商品id：<input type="text"><br><br>
                                商品名称：<input type="text"><br><br>
                                商品库存：<input type="text"><br><br>
                                商品价格：<input type="text"><br><br>
                                商品状态：<input type="text"><br><br>
                                商品图片：<input type="url" placeholder="请填写图片地址"><br><br>
                                商品已售：<input type="text"><br><br>
                                <button class="add_btn">添加</button><br><br>
                                <input type="text" placeholder="按照商品名称查询">
                                <button class="query_btn">查询</button>
                            </div>
                            <table cellspacing="0" border="1px">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>图片</th>
                                        <th>名称</th>
                                        <th>价格</th>
                                        <th>库存</th>
                                        <th>已售</th>
                                        <th>状态</th>
                                        <th>创建时间</th>
                                        <th>更新时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                            <div id="demo-laypage-normal-1"></div>
                        </div>
                        <div class="chart tab">
                            <div></div>
                            <h1>2222</h1>
                        </div>
                        <div class="personCenter tab">
                            <h1>555555</h1>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-footer">
            <!-- 底部固定区域 -->
            底部固定区域
        </div>
    </div>

    <script src="//unpkg.com/layui@2.11.2/dist/layui.js"></script>
    <script src="https://code.jquery.com/jquery-3.7.1.js"></script>

    <script>
        //JS 
        layui.use(['element', 'layer', 'util'], function () {
            var element = layui.element;
            var layer = layui.layer;
            var util = layui.util;
            var $ = layui.$;

            //头部事件
            util.event('lay-header-event', {
                menuLeft: function (othis) { // 左侧菜单事件
                    layer.msg('展开左侧菜单的操作', { icon: 0 });
                },
                menuRight: function () {  // 右侧菜单事件
                    layer.open({
                        type: 1,
                        title: '更多',
                        content: '<div style="padding: 15px;">处理右侧面板的操作</div>',
                        area: ['260px', '100%'],
                        offset: 'rt', // 右上角
                        anim: 'slideLeft', // 从右侧抽屉滑出
                        shadeClose: true,
                        scrollbar: false
                    });
                }
            });
        });

        // 获取本地存储内容
        var record = JSON.parse(localStorage.getItem('record'))
        $('.layui-hide a span').html(record.uname)


        // 侧边tab切换
        function tabChange() {
            $('.tabBtn').eq(0).css({
                'color': 'white',
                'background-color': '#16baaa'
            })

            $('.tabBtn').on('click', function () {
                $('.tabBtn').eq(0).css({
                    'color': 'white',
                    'background-color': 'rgba(0,0,0,0)'
                })
                var index = $(this).index()
                $('.tab').removeClass('show')
                $('.tab').eq(index).addClass('show')
            })
            // 默认第一个显示
            $('.tab').eq(0).addClass('show')
        }
        tabChange()

        // 商品管理页面的分页符
        layui.use(function () {
            var laypage = layui.laypage;
            // 普通用法
            laypage.render({
                elem: 'demo-laypage-normal-1',
                count: 60 // 数据总数
            });
        });


        // 商品数据渲染
        function data() {
            $.ajax({
                type: 'GET',
                url: 'https://result.eolink.com/NUPerrp561bd641f17dcca996d57132f30db1b99d775b77?uri=/goodsx/listByPage',
                dataType: 'json',
                success: (rel) => {
                    console.log(rel.data);
                    $('.goods tbody').html('')
                    rel.data.forEach((item, index) => {
                        $('.goods tbody').append(`
                        <tr>
                            <th>${item.id}</th>
                            <th><img src="${item.img}" alt=""></th>
                            <th>${item.goods_name}</th>
                            <th>${item.price}</th>
                            <th>${item.stock}</th>
                            <th>${item.sold}</th>
                            <th></th>
                            <th>${new Date(item.created_at).toLocaleString()}</th>
                            <th>${new Date(item.updated_at).toLocaleString()}</th>
                            <td>
                                <button>删除</button>
                                <button>修改</button>
                                <button>查看详情</button>
                            </td>
                        </tr>`)
                    });

                },
                error: (e) => {
                    console.log(e);
                }
            })
        }
        data()


        // 添加数据
        $('.add_btn').click(() => {
            $.ajax({
                type: 'POST',
                url: 'https://result.eolink.com/NUPerrp561bd641f17dcca996d57132f30db1b99d775b77?uri=/goodsx/add',
                data: {
                    goods_name: $('.goods>div>input').eq(1).val(),
                    stock: $('.goods>div>input').eq(2).val(),
                    price: $('.goods>div>input').eq(3).val(),
                    status: $('.goods>div>input').eq(4).val(),
                    sold: $('.goods>div>input').eq(5).val(),
                    img: $('.goods>div>input').eq(6).val()
                },
                dataType: 'json',
                success: (rel) => {
                    console.log(rel);
                    data()
                },
                error: (e) => {
                    console.log(e);

                }

            })
        })
    </script>
</body>

</html>